package com.wuyou.controller;

import cn.hutool.core.bean.BeanUtil;
import com.wuyou.domain.Result;
import com.wuyou.em.ResultEnum;
import com.wuyou.entity.User;
import com.wuyou.service.IUserService;
import com.wuyou.service.bo.UserBO;
import com.wuyou.vo.UserVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘棋城
 * @since 2022-09-30
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    //查询用户
    @GetMapping("getUser")
    public Result<User> getUser(){
        List<User> list = userService.list();
        return Result.OK().setData(list);
    }

    //id查询用户
    @PostMapping("getUserById")
    public Result<User> getUserById(Long userId){
        User user = userService.getById(userId);
        return Result.OK().setData(user);
    }

    //注册
    @PostMapping("userAdd")
    @ApiOperation(value = "用户注册")
    @ApiImplicitParams({@ApiImplicitParam(name = "userVO",value = "用户实体类",required = true,
            dataTypeClass = User.class, paramType = "body")})
    public Result userAdd(@RequestBody UserVO userVO){

        if (BeanUtil.isEmpty(userVO)) {
            return Result.R(ResultEnum.MESSAGE_ERROR);
        }
        UserBO bo = UserBO.builder().build();
        BeanUtils.copyProperties(userVO, bo);
        int i = userService.userAdd(bo);
        return Result.OK();
    }


}
